package events

import lua "github.com/yuin/gopher-lua"
import "../luahelper"

func RegisterFunc_4lua(l *lua.LState) {
	table_func := l.SetFuncs(l.NewTable(), map[string]lua.LGFunction{
		"Register": Register_4lua,
		"Trigger": Trigger_4lua,
	})

	l.SetGlobal("Event", table_func)
}

func Register_4lua(l *lua.LState) int {
	// Event.Register("Exit", function(){ })

	event_name := l.ToString(1)
	event_func := l.ToFunction(2)

	funcId:= luahelper.RegisterLuaCallback(l, "Event", event_func)

	Register(event_name, func() {
		luahelper.SafeCallLuaCallback(l, funcId)
	})
	return 0
}

func Trigger_4lua(l *lua.LState) int {
	// Event.Trigger("Exit")

	event_name := l.ToString(1)
	Trigger(event_name)

	return 0
}
